package createobject;
//import java.io.File;
//import java.io.FileNotFoundException;
import java.util.Scanner;
import point.FixedMass;
import point.Mass;
import point.ObjectPoint;
import environment.Simulation;
//import force.Centermass;
//import force.Gravity;
//import force.Viscosity;
//import force.Wallrepulsion;
import line.Line;
import line.Muscle;
import line.Spring;
import line.Bar;
//import force.Gravity;

/**
 * @author Michael Deng, Xi Du
 *
 */
public class ObjectFactory extends Factory {
//    /**
//     * @param sim Simulation
//     * @param modelFile File
//     */
//    public void loadModel(Simulation sim, File modelFile) {
//        try {
//            Scanner input = new Scanner(modelFile);
//            while (input.hasNext()) {
//                Scanner line = new Scanner(input.nextLine());
//                if (line.hasNext()) {
//                    String type = line.next();
//                    createsPhysicsObject(sim, line, type);
//                }
//            }
//            input.close();
//        } catch (FileNotFoundException e) {
//            // should not happen because File came from user selection
//            e.printStackTrace();
//        }
//    }

//    /**
//     * @param sim Simulation
//     * @param line line of read text
//     * @param type type of object to create
//     */
//    private void createsPhysicsObject (Simulation sim, Scanner line, String type) {
//        if (type.equals("mass")) {
//            sim.add(massCommand(line));
//        } else if (type.equals("spring")) {
//            sim.add(springCommand(line, sim));
//        } else if (type.equals("muscle")) {
//            sim.add(muscleCommand(line, sim));
//        } else {
//            // Do nothing
//        }
//    }

   
    @Override
    void createObject (Simulation sim, Scanner line, String type) {
        if (type.equals("mass")) {
            sim.add(massCommand(line));
        } else if (type.equals("spring")) {
            sim.add(springCommand(line, sim));
        } else if (type.equals("muscle")) {
            sim.add(muscleCommand(line, sim));
        } else {
            // Do nothing
        }
    }
    
    
    /**
     * @param line a scanner
     * @return a mass
     */
    private ObjectPoint massCommand(Scanner line) {
        int id = line.nextInt();
        double x = line.nextDouble();
        double y = line.nextDouble();
        double mass = line.nextDouble();
        if (mass >= 0) {
            return new Mass(id, x, y, mass);
        } else {
            return new FixedMass(id, x, y, mass);
        }
    }
    /**
     * @param line scanner
     * @param sim simulation
     * @return returns a line
     */
    private Line springCommand(final Scanner line, final Simulation sim) {
        int m1 = line.nextInt();
        int m2 = line.nextInt();
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        if (ks >= 0) {
            return new Spring(sim.getMass(m1), sim.getMass(m2), restLength, ks);
        } else {
            return new Bar(sim.getMass(m1),
                    sim.getMass(m2), restLength, 1000000);
        }
    }

    /**
     * @param line scanner
     * @param sim simulation
     * @return returns a muscle
     */
    private Muscle muscleCommand(final Scanner line, final Simulation sim) {
        int m1 = line.nextInt();
        int m2 = line.nextInt();
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        double amp = line.nextDouble();
        return new Muscle(sim.getMass(m1),
                sim.getMass(m2), restLength, ks, amp);
    }
}
